package com.amber.proyecto.envia.imagenes.sw;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.amber.proyecto.envia.imagenes.sw.mibd.BD;
import com.amber.proyecto.envia.imagenes.sw.utils.Estado;
import com.amber.proyecto.envia.imagenes.sw.utils.ImagenParcelable;
import com.amber.proyecto.envia.imagenes.sw.utils.Variables;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class Mapa extends android.support.v4.app.FragmentActivity implements LocationListener{
	  //
	 private Button ivAtrasMapa;
	 private Button ivInicioMapa;
	 private Button btnSatCalles;
	 private boolean satelite = false;
	 //static final LatLng OAX = new LatLng(17.063021, -96.7202);
	 private LatLng estado; //= new LatLng(Variables.estadoSeleccionado.getLatitud(), Variables.estadoSeleccionado.getLongitud());
	 private GoogleMap map;
	 private Bundle bundle;
	 private ArrayList<ImagenParcelable> imagenes = new ArrayList<ImagenParcelable>();
	 private HashMap<Marker, ImagenParcelable> datosImagen = new HashMap<Marker, ImagenParcelable>();
	 private String queryFinal;
	 private LocationManager locationManager;


	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.mapa);



	    bundle = getIntent().getExtras();
	   imagenes = bundle.getParcelableArrayList("imagenes");
	   queryFinal = bundle.getString("queryFinal");
	   
	   btnSatCalles = (Button)findViewById(R.id.btnSateliteMapa);
	   btnSatCalles.setOnClickListener(btnSatCallesPres);
	   
	   ivAtrasMapa = (Button)findViewById(R.id.btnAtrasMapa);
	   ivAtrasMapa.setOnClickListener(ivAtrasMapaPres);
	   //imprimeLugares();
	   
	   ivInicioMapa = (Button)findViewById(R.id.btnInicioMapa);
	   ivInicioMapa.setOnClickListener(ivInicioMapaPres);
	   
	   BD bd = new BD(this);
	   Estado estadoSel = new Estado();
	   estadoSel = bd.obtieneEstado(bundle.getInt("estadoSel"));
	   bd.close();
	   estado = new LatLng(estadoSel.getLatitud(), estadoSel.getLongitud());
	   
	   
	    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapa))
	        .getMap();
	    map.clear();
		  System.gc();
		  Marker oax = map.addMarker(new MarkerOptions().position(estado)
			        .title(Variables.estadoSeleccionado.getNombreEstado())
			         .snippet(Variables.estadoSeleccionado.getInicialesEstado())
			        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
	    /*Marker oax = map.addMarker(new MarkerOptions().position(OAX)
		  
		        .title("Oaxaca")
		         .snippet("Oaxaca de Juárez")
		        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
*/
	    for (int i=0; i<imagenes.size(); i++){
			  Log.i("imagen #",i+".");
			  Marker mark = map.addMarker(new MarkerOptions().position(
					  new LatLng(imagenes.get(i).getLatitud(), imagenes.get(i).getLongitud()))
				.title("Categoría: "+imagenes.get(i).getNombreCategoria())
				.snippet(imagenes.get(i).getComentario())
				.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
					  );
			  datosImagen.put(mark, imagenes.get(i));
			  
			  //map.addMarker(options)
		  }
	    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
	    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
		    map.setMyLocationEnabled(true);
		    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		    Criteria criteria = new Criteria();
		    
	        // Getting the name of the best provider
	        String provider = locationManager.getBestProvider(criteria, true);
	
	        // Getting Current Location
	        Location location = locationManager.getLastKnownLocation(provider);
	
	        if(location!=null){
	            onLocationChanged(location);
	        }
	        locationManager.requestLocationUpdates(provider, 20000, 0, this);
	    
	    }

	    //map.moveCamera(CameraUpdateFactory.newLatLngZoom(OAX, 12));
	    map.moveCamera(CameraUpdateFactory.newLatLngZoom(estado, 12));

	    map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
	    
	    map.setOnMarkerClickListener(markerListener);
	    map.setOnInfoWindowClickListener(info);
	    
	    
	  }

	public void onLowMemory() {
		super.onLowMemory();
		System.gc();
	}
	  
	  private void limpiar(){
		  System.gc();
	  }
	  
	  private OnClickListener ivInicioMapaPres = new OnClickListener() {

		public void onClick(View v) {
			System.gc();
			Intent intent = new Intent();
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setClass(Mapa.this, Principal.class);
			startActivity(intent);
			finish();
		}
	};
	  
	  private OnClickListener ivAtrasMapaPres = new OnClickListener() {
		public void onClick(View v) {
			System.gc();
			Intent intent = new Intent();
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setClass(Mapa.this, Busca.class);
			startActivity(intent);
			finish();
		}
	};
	  
	  private OnClickListener btnSatCallesPres = new OnClickListener() {
		
		public void onClick(View v) {
			System.gc();
			if (satelite == false){
				btnSatCalles.setText("Calles");
				map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
				satelite = true;
				btnSatCalles.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.calles32x32), null, null);
			}
			else{
				btnSatCalles.setText("Satélite");
				map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
				btnSatCalles.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.satelite32x32), null, null);
				satelite = false;
			}
			
		}
	};
	  
	  private OnMarkerClickListener markerListener = new OnMarkerClickListener() {
		public boolean onMarkerClick(Marker marker) {
			Toast.makeText(Mapa.this, marker.getSnippet(), Toast.LENGTH_LONG).show();
			return false;
		}
	};
	
	
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
    
	  public boolean onCreateOptionsMenu(Menu menu) {

	    return true;
	  }

	  private OnInfoWindowClickListener info = new OnInfoWindowClickListener() {
		
		public void onInfoWindowClick(Marker marker) {
			//Log.i("Seleccionado ", datosImagen.get(marker).getNombreImagen());
			if (!marker.getId().equals("m0")){
				limpiar();
				Intent intent = new Intent();
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.putExtra("datosImagen", datosImagen.get(marker));
				intent.putExtra("queryFinal", queryFinal);
				intent.setClass(Mapa.this, MuestraInfo.class);
				startActivity(intent);
				finish();
			}
		}
	};




	public void onLocationChanged(Location location) {
		double latitude = location.getLatitude();
		 
        // Getting longitude of the current location
        double longitude = location.getLongitude();
 
        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);
		
	}


	public void onProviderDisabled(String provider) {

		
	}


	public void onProviderEnabled(String provider) {

		
	}


	public void onStatusChanged(String provider, int status, Bundle extras) {

		
	}}
